package com.example.shop_2305.minefg

import android.app.ActionBar.LayoutParams
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.PopupWindow
import android.widget.RatingBar
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.Order
import com.example.lib_base.mvvm.BaseFragment
import com.example.lib_base.param.PComment
import com.example.shop_2305.R
import com.example.shop_2305.adapter.ImgAdapter
import com.example.shop_2305.adapter.OrderAdapter
import com.example.shop_2305.databinding.FragmentOrder1Binding
import com.example.shop_2305.databinding.FragmentOrder2Binding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector


class Order2Fragment  : BaseFragment<FragmentOrder2Binding, OrderViewModel>() {
    lateinit var orderAdapter: OrderAdapter
    var imgs = mutableListOf<String>()
    lateinit var imgAdapter: ImgAdapter

    var list = mutableListOf("java","kotlin","c")
    override fun initView() {
        Log.d("sss", "initView: ${list.toString()}")
        orderAdapter = OrderAdapter()
        binding.rv.adapter = orderAdapter
        binding.rv.layoutManager = LinearLayoutManager(context)

        viewModel.order("1",20,1)

        //点击催发货，弹出评论框进行评论
        orderAdapter.addChildClickViewIds(R.id.order_tv2)
        orderAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.order_tv2->{
                    //弹出评论框，可以是跳转到新的评论界面，也可以是popupwindow
                    showPopupWindow(orderAdapter.data[position])
                }
            }
        }

        imgAdapter = ImgAdapter()
    }

    private fun showPopupWindow(order: Order) {



        val popupWindow = PopupWindow()
        popupWindow.width = LayoutParams.MATCH_PARENT
        popupWindow.height = LayoutParams.WRAP_CONTENT

        val inflate = LayoutInflater.from(context).inflate(R.layout.pop_msg, null)

        val rv = inflate.findViewById<RecyclerView>(R.id.rv_img)
        rv.adapter = imgAdapter
        rv.layoutManager = GridLayoutManager(context,3)
        //点击上传图片，可以在页面上展示
        inflate.findViewById<Button>(R.id.btn_upload).setOnClickListener {
            PictureSelector.create(this,300).selectPicture()
        }
        //点击发送，实现发送评论
        inflate.findViewById<Button>(R.id.btn_send).setOnClickListener {
            val pComment = PComment()
            pComment.comment = inflate.findViewById<EditText>(R.id.et).text.toString()
            pComment.orderNo = order.orderId
            pComment.productId = order.orderInfoList[0].productId
            pComment.productScore = inflate.findViewById<RatingBar>(R.id.rb).rating.toInt()
            pComment.serviceScore = inflate.findViewById<RatingBar>(R.id.rb).rating.toInt()
            pComment.pics = imgs.toString()

            viewModel.sendComment(pComment)

            popupWindow.dismiss()
        }

        popupWindow.setOnDismissListener {
            imgs.clear()
        }
        popupWindow.contentView = inflate
        popupWindow.isOutsideTouchable = true
        popupWindow.isFocusable = true
        popupWindow.showAtLocation(binding.rv,Gravity.BOTTOM,0,0)

    }

    override fun initData() {
        viewModel.orderSuccess.observe(this){
            if(it.code==200){
                orderAdapter.data.clear()
                orderAdapter.data.addAll(it.data.list)
                orderAdapter.notifyDataSetChanged()
            }
        }
        viewModel.sendSuccess.observe(this){
            ToastUtils.showLong(it.message)
        }
    }

    override fun getViewModel(): Class<OrderViewModel>  = OrderViewModel::class.java

    override fun getLayout(): Int = R.layout.fragment_order2

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==300&&resultCode==Activity.RESULT_OK){
            val pictureBean = data?.extras?.get(PictureSelector.PICTURE_RESULT) as PictureBean
            imgs.add(pictureBean.path)
            imgAdapter.data.clear()
            imgAdapter.data.addAll(imgs)
            imgAdapter.notifyDataSetChanged()

        }
    }

}